package com.csxy.six.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.csxy.six.R;
import com.csxy.six.entity.AnnouncementAttachment;
import com.csxy.six.service.AnnouncementAttachmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 公告附件控制器
 * 提供公告附件相关的RESTful API
 */
@RestController
@RequestMapping("/annoservice/attachment")
@Tag(name = "附件管理", description = "提供公告附件的增删改查接口")
public class AnnouncementAttachmentController {

    @Autowired
    private AnnouncementAttachmentService attachmentService;

    //根据公告ID获取附件列表
    @Operation(summary = "根据公告ID获取附件列表")
    @GetMapping("/announcement/{announceId}")
    public R getByAnnounceId(@PathVariable Integer announceId) {
        return R.ok().data("list", attachmentService.getByAnnounceId(announceId));
    }

    //上传附件

    @Operation(summary = "上传附件")
    @PostMapping
    public R save(@RequestBody AnnouncementAttachment attachment) {
        if (attachment.getAnnounceId() == null || StringUtils.isEmpty(attachment.getFileUrl()) || StringUtils.isEmpty(attachment.getFileName())) {
            return R.error().message("缺少必要的参数: announceId, fileUrl 或 fileName");
        }
        return attachmentService.save(attachment) ? R.ok() : R.error();
    }

    //删除附件
    @Operation(summary = "删除附件")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        return attachmentService.removeById(id) ? R.ok() : R.error();
    }

}